﻿using System;
using System.Collections.ObjectModel;

using GalaSoft.MvvmLight.Command;
using Rio.Manager.Application.ViewModel.BaseClass;
using Rio.Framework.Localisation;
using Rio.Manager.Application.ViewModel.Locator;
using System.Collections.Generic;
using Rio.Manager.Application.ViewModel.Administration;
using Rio.Manager.Application.Model;
using Rio.Applications.Web.Services;
using System.ServiceModel.DomainServices.Client;

namespace Rio.Manager.Application.ViewModel.GeneralTabs
{
    public class AdministrationTabViewModel : TabViewModelBase
    {
        #region Fields

        private string _generalItemsPlanLabel;
        private string _generalParametersDescription;
        private ObservableCollection<ObservableKeyValuePair<string, string>> _generalParametersItems;
        private string _generalParametersLabel;
        private ObservableKeyValuePair<string, string> _generalParametersSelectedItem;        
        private string _usersLabel;
        private string _workflowLabel;
        private string _connectorLabel;
        private string _connectorDescription;
        private List<ObservableKeyValuePair<string, string>> _connectorsCategoryItems;
        private ObservableKeyValuePair<string, string> _connectorManageCategorySelectedItem;
        private string _BarCodeLabel;
        private string _BarCodeDescription;
        private List<ObservableKeyValuePair<string, string>> _BarCodeCategoryItems;
        private ObservableKeyValuePair<string, string> _BarCodeManageCategorySelectedItem;
        private string _groupsLabel;
        private string _GroupDescription;
        private List<ObservableKeyValuePair<string, string>> _GroupCategoryItems;
        private ObservableKeyValuePair<string, string> _GroupManageCategorySelectedItem;

        #endregion Fields

        #region Constructors

        public AdministrationTabViewModel()
            : base()
        {
            TabIcon = new Uri("/Rio.Manager.Application;component/Assets/Images/administration.png", UriKind.Relative);
        }

        #endregion Constructors

        #region Properties

        public string GeneralItemsPlanLabel
        {
            get { return _generalItemsPlanLabel; }
            set
            {
                _generalItemsPlanLabel = value;
                RaisePropertyChanged("GeneralItemsPlanLabel");
            }
        }

        public string GeneralParametersDescription
        {
            get { return _generalParametersDescription; }
            set
            {
                _generalParametersDescription = value;
                RaisePropertyChanged("GeneralParametersDescription");
            }
        }

        public ObservableCollection<ObservableKeyValuePair<string, string>> GeneralParametersItems
        {
            get { return _generalParametersItems; }
            set
            {
                _generalParametersItems = value;
                RaisePropertyChanged("GeneralParametersItems");
            }
        }

        public string GeneralParametersLabel
        {
            get { return _generalParametersLabel; }
            set
            {
                _generalParametersLabel = value;
                RaisePropertyChanged("GeneralParametersLabel");
            }
        }

        public ObservableKeyValuePair<string, string> GeneralParametersSelectedItem
        {
            get { return _generalParametersSelectedItem; }
            set
            {
                _generalParametersSelectedItem = value;
                RaisePropertyChanged("GeneralParametersSelectedItem");
            }
        }        

        public RelayCommand InitializationCommand
        {
            get;
            set;
        }

        public RelayCommand OpenNewGeneralParametersTabCommand
        {
            get;
            set;
        }

        public string UsersLabel
        {
            get { return _usersLabel; }
            set
            {
                _usersLabel = value;
                RaisePropertyChanged("UsersLabel");
            }
        }

        public string WorkflowLabel
        {
            get { return _workflowLabel; }
            set
            {
                _workflowLabel = value;
                RaisePropertyChanged("WorkflowLabel");
            }
        }

        public string ConnectorLabel
        {
            get { return _connectorLabel; }
            set
            {
                _connectorLabel = value;
                RaisePropertyChanged("ConnectorLabel");
            }
        }

        public string ConnectorDescription
        {
            get { return _connectorDescription; }
            set
            {
                _connectorDescription = value;
                RaisePropertyChanged("ConnectorDescription");
            }
        }

        public List<ObservableKeyValuePair<string, string>> ConnectorManageCategoryItems
        {
            get { return _connectorsCategoryItems; }
            set
            {
                _connectorsCategoryItems = value;
                RaisePropertyChanged("ConnectorManageCategoryItems");
            }
        }

        public ObservableKeyValuePair<string, string> ConnectorManageCategorySelectedItem
        {
            get { return _connectorManageCategorySelectedItem; }
            set
            {
                _connectorManageCategorySelectedItem = value;
                RaisePropertyChanged("ConnectorManageCategorySelectedItem");
            }
        }

        public RelayCommand OpenNewConnectorCategoryItemCommand
        {
            get;
            set;
        }

        public string BarCodeLabel
        {
            get { return _BarCodeLabel; }
            set
            {
                _BarCodeLabel = value;
                RaisePropertyChanged("BarCodeLabel");
            }
        }

        public string BarCodeDescription
        {
            get { return _BarCodeDescription; }
            set
            {
                _BarCodeDescription = value;
                RaisePropertyChanged("BarCodeDescription");
            }
        }

        public List<ObservableKeyValuePair<string, string>> BarCodeCategoryItems
        {
            get { return _BarCodeCategoryItems; }
            set
            {
                _BarCodeCategoryItems = value;
                RaisePropertyChanged("BarCodeCategoryItems");
            }
        }

        public ObservableKeyValuePair<string, string> BarCodeManageCategorySelectedItem
        {
            get { return _BarCodeManageCategorySelectedItem; }
            set
            {
                _BarCodeManageCategorySelectedItem = value;
                RaisePropertyChanged("BarCodeManageCategorySelectedItem");
            }
        }

        public RelayCommand OpenNewBarCodeCategoryItemCommand
        {
            get;
            set;
        }

        public string GroupsLabel
        {
            get { return _groupsLabel; }
            set
            {
                _groupsLabel = value;
                RaisePropertyChanged("GroupsLabel");
            }
        }

        public string GroupDescription
        {
            get { return _GroupDescription; }
            set
            {
                _GroupDescription = value;
                RaisePropertyChanged("GroupDescription");
            }
        }

        public List<ObservableKeyValuePair<string, string>> GroupCategoryItems
        {
            get { return _GroupCategoryItems; }
            set
            {
                _GroupCategoryItems = value;
                RaisePropertyChanged("GroupCategoryItems");
            }
        }

        public ObservableKeyValuePair<string, string> GroupManageCategorySelectedItem
        {
            get { return _GroupManageCategorySelectedItem; }
            set
            {
                _GroupManageCategorySelectedItem = value;
                RaisePropertyChanged("GroupManageCategorySelectedItem");
            }
        }

        public RelayCommand OpenNewGroupCategoryItemCommand
        {
            get;
            set;
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Inits the commands.
        /// </summary>
        public override void InitCommands()
        {
            InitializationCommand = new RelayCommand(Initialization);
            OpenNewGeneralParametersTabCommand = new RelayCommand(OpenNewGeneralParametersTab);
            OpenNewConnectorCategoryItemCommand = new RelayCommand(OpenNewConnectorCategoryItem);
            OpenNewBarCodeCategoryItemCommand = new RelayCommand(OpenNewBarCodeCategoryItem);
            OpenNewGroupCategoryItemCommand = new RelayCommand(OpenNewGroupCategoryItem);
        }

        /// <summary>
        /// Initializations this instance.
        /// </summary>
        public void Initialization()
        {
            InitLabels();
            InitCategories();
        }

        /// <summary>
        /// Inits the categories.
        /// </summary>
        private void InitCategories()
        {
            //General parameters categories.
            GeneralParametersItems = new ObservableCollection<ObservableKeyValuePair<string, string>>();
            GeneralParametersItems.Add(new ObservableKeyValuePair<string, string>("ListAllGeneralParameters", Resx.GetLabel(ResxRioManager.LIST_ALL_GENERAL_PARAMETERS)));

            //General connector categories.
            ConnectorManageCategoryItems = new List<ObservableKeyValuePair<string, string>>();
            ConnectorManageCategoryItems.Add(new ObservableKeyValuePair<string, string>("Connector_Add", Resx.GetLabel(ResxRioManager.CONNECTOR_ADD)));
            ConnectorManageCategoryItems.Add(new ObservableKeyValuePair<string, string>("Connector_Manage", Resx.GetLabel(ResxRioManager.CONNECTOR_MANAGE)));

            //Category BarCode menu.
            BarCodeCategoryItems = new List<ObservableKeyValuePair<string, string>>();
            BarCodeCategoryItems.Add(new ObservableKeyValuePair<string, string>("BarCode_Add", Resx.GetLabel(ResxRioManager.BARCODE_ADD)));
            BarCodeCategoryItems.Add(new ObservableKeyValuePair<string, string>("BarCode_Manage", Resx.GetLabel(ResxRioManager.BARCODE_MANAGE)));

            //Category Group menu.
            GroupCategoryItems = new List<ObservableKeyValuePair<string, string>>();
            GroupCategoryItems.Add(new ObservableKeyValuePair<string, string>("Group_Association", Resx.GetLabel(ResxRioManagerGroup.GROUP_ASSOCIATION) + " " + Resx.GetLabel(ResxRioManagerGroup.GROUP_RIO_AD)));
            GroupCategoryItems.Add(new ObservableKeyValuePair<string, string>("Group_Synchronization", Resx.GetLabel(ResxRioManagerGroup.GROUP_CORRESPONDENCE) + " " + Resx.GetLabel(ResxRioManagerGroup.GROUP_RIO_AD)));
        }

        /// <summary>
        /// Inits the labels.
        /// </summary>
        public override void InitLabels()
        {
            UsersLabel = Resx.GetLabel(ResxCommon.USERS);            
            WorkflowLabel = Resx.GetLabel(ResxCommon.WORKFLOW);
            GeneralItemsPlanLabel = Resx.GetLabel(ResxRioManager.GENERAL_ITEMS_PLAN);
            ConnectorLabel = Resx.GetLabel(ResxCommon.CONNECTOR_BLOC);
            ConnectorDescription = Resx.GetLabel(ResxRioManager.CONNECTOR_CATEGORY_DESCRIPTION);
            GeneralParametersLabel = Resx.GetLabel(ResxRioManager.GENERAL_PARAMETERS);
            GeneralParametersDescription = Resx.GetLabel(ResxRioManager.GENERAL_PARAMETERS_DESCRIPTION);
            BarCodeLabel = Resx.GetLabel(ResxCommon.BARCODE_BLOC);
            BarCodeDescription = Resx.GetLabel(ResxRioManager.BARCODE_CATEGORY_DESCRIPTION);
            GroupsLabel = Resx.GetLabel(ResxCommon.GROUPS);
            GroupDescription = Resx.GetLabel(ResxRioManagerGroup.GROUP_CATEGORY_DESCRIPTION);
        }

        /// <summary>
        /// Opens the new connector parameters tab.
        /// </summary>
        public void OpenNewConnectorCategoryItem()
        {
            if (ConnectorManageCategorySelectedItem != null)
            {
                switch (ConnectorManageCategorySelectedItem.Key)
                {
                    case "Connector_Add":
                        //Open the connector added page.
                        ConnectorAddTabViewModel newTabAdd = ViewModelLocator.ConnectorAddTabStatic;
                        newTabAdd = ViewModelLocator.ConnectorAddTabStatic;
                        newTabAdd.ConnectorInformationToUpdate = null;
                        newTabAdd.SelectedOrganization = null;
                        newTabAdd.SelectedFolderType = null;
                        newTabAdd.IsModifyMode = false;
                        AddTab(newTabAdd);                       
                        break;
                    case "Connector_Manage":                       
                        ConnectorTabViewModel newTabManage = ViewModelLocator.ConnectorTabStatic;
                        AddTab(newTabManage);
                        break;
                }

                ConnectorManageCategorySelectedItem = null;
            }
        }        

        /// <summary>
        /// Opens the new general parameters tab.
        /// </summary>
        private void OpenNewGeneralParametersTab()
        {
            if (GeneralParametersSelectedItem != null)
            {
                TabViewModelBase newTab = null;

                switch (GeneralParametersSelectedItem.Key)
                {
                    case "ListAllGeneralParameters":
                        newTab = ViewModelLocator.GeneralParametersTabStatic;
                        break;
                }

                if (newTab != null)
                {
                    Action<TabViewModelBase> actionToProcess = (tab) =>
                        {
                            tab.SelectedOrganization = this.SelectedOrganization;
                            tab.SelectedFolderType = this.SelectedFolderType;
                        };

                    AddTab(newTab, actionToProcess);
                }

                GeneralParametersSelectedItem = null;
            }
        }

        /// <summary>
        /// Opens the new barcode parameters tab.
        /// </summary>
        public void OpenNewBarCodeCategoryItem()
        {
            if (BarCodeManageCategorySelectedItem != null)
            {
                switch (BarCodeManageCategorySelectedItem.Key)
                {
                    case "BarCode_Add":
                        //Open the barcode added page.
                        BarCodeAddTabViewModel newTabAdd = ViewModelLocator.BarCodeAddTabStatic;
                        newTabAdd.IsModifyMode = false;
                        newTabAdd.SelectedDisplayBarCodeFunction = new DisplayBarCodeFunction();
                        newTabAdd.SelectedDisplayBarCodeTypologyType = new DisplayBarCodeType();
                        newTabAdd.SelectedOrganization = null;
                        newTabAdd.SelectedFolderType = null;
                        AddTab(newTabAdd);
                        break;
                    case "BarCode_Manage":
                        //Open the barcode manage page.
                        BarCodeTabViewModel newTabManage = ViewModelLocator.BarCodeTabStatic;
                        AddTab(newTabManage);

                        break;
                }

                BarCodeManageCategorySelectedItem = null;
            }
        }

        /// <summary>
        /// Open group correspondence table.
        /// </summary>
        public void OpenNewGroupCategoryItem()
        {
            if (GroupManageCategorySelectedItem != null)
            {
                switch (GroupManageCategorySelectedItem.Key)
                {
                    case "Group_Synchronization":
                        //Open the barcode added page.
                        GroupSynchronizationTabViewModel newTabSynchronization = ViewModelLocator.GroupSynchronizationTabStatic;

                        AddTab(newTabSynchronization);
                        break;
                    case "Group_Association":
                        //Open the barcode manage page.
                        GroupAssociationTabViewModel newTabAssociation = ViewModelLocator.GroupAssociationTabStatic;
                        AddTab(newTabAssociation);
                        break;
                }

                GroupManageCategorySelectedItem = null;
            }
        }

        #endregion Methods
    }
}